package com.btb.core.schedule;

public abstract class Job implements Runnable {

	/**
	 * 是否循环执行
	 */
	private volatile boolean loop;

	private IDelayStrategy delayStrategy;

	public Job() {
	}

	public Job(final boolean loop) {
		this.loop = loop;
	}

	public Job(final IDelayStrategy delayStrategy) {
		this.loop = true;
		this.delayStrategy = delayStrategy;
	}

	@Override
	public void run() {
		do {
			if (this.delayStrategy != null) {
				this.delayStrategy.delay(!execute());
			} else {
				execute();
			}
		} while (this.loop && !Thread.currentThread().isInterrupted());
	}

	public void shutdown() {
		this.loop = false;
	}

	public boolean isShutdown() {
		return !this.loop;
	}

	public abstract boolean execute();
}